#include "capture.h"
#include <opencv2/opencv.hpp>
#include <QFileDialog>
//using namespace cv;

capture::capture(QWidget *parent) : QWidget(parent)
{
    cap=new cv::VideoCapture;
    timer=new QTimer(this);
    frame=new cv::Mat;
    videoaddress="res\\1.mp4";
    cap->open(videoaddress.toStdString());
    connect(this,SIGNAL(start()),this,SLOT(startTimer()));
    connect(this,SIGNAL(stop()),this,SLOT(stopTimer()));
}
void capture::setVideoAddress(QString videoaddress)
{
    this->videoaddress=videoaddress;
}
void capture::openVideo()
{
    QString file=QFileDialog::getOpenFileName(this,"选择视频",QCoreApplication::applicationDirPath()+"//res","*.mp4 *.avi");
    if(file!="")
    {
        setVideoAddress(file);
        cap->open(videoaddress.toStdString());
        cv::Mat mat;
        cap->read(mat);
        emit stop();
        emit switchSource();
    }


}

void capture::startTimer()
{
    timer->start(0);
}

void capture::stopTimer()
{
    timer->stop();
}
cv::Mat* capture::readFrame()
{
    cap->read(*frame);
    if(frame->empty())
    {
        cap->release();
        emit stop();
        *frame=cv::imread("res\\end.jpg");
    }
    return frame;
}

void capture::openCamera()
{
    cap->open(0);
    emit stop();
    emit switchSource();
}
capture::~capture()
{
    delete frame;
    delete cap;
}
